package ru.cod.rs.object;

import ru.cod.rs.enums.Actions;
import ru.cod.rs.object.actions.AnimatedActions;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;

public class AnimatedObject extends StateObject implements AnimatedActions {

	protected int frames;
	/*
	 * TODO:Реализовать использование action(s) пока у нас 1-2 действия
	 * (movement to the left and to the right) либо проверить, что есть
	 */
	protected int actions;
	protected boolean animated;

	private int frame;
	protected Actions action;

	// ////////////////
	public AnimatedObject(Bitmap b) {
		super(b);
		frame = 0;
		action = Actions.Right;
	}

	public AnimatedObject(Bitmap b, int x, int y) {
		super(b, x, y);
		frame = 0;
		action = Actions.Right;
	}

	// ///////////////
	public void update() {
		frame = ++frame % frames;
	}

	@Override
	public void draw(Canvas c) {
		if (animated) {
			update();
		}

		int srcX = frame * width, srcY = action.action() * height;
		Rect rectI = new Rect(srcX, srcY, srcX + width, srcY + height);
		RectF rectP = new RectF(x, y, x + width, y + height);
		c.drawBitmap(image, rectI, rectP, null);
	}

	// ////////////////////////////////////////////////////////////////
	/**
	 * @return the animated
	 */
	public boolean isAnimated() {
		return animated;
	}

	/**
	 * @param animated
	 *            the animated to set
	 */
	public void setAnimated(boolean animated) {
		this.animated = animated;
	}

	public void setFrames(byte aFrames) {
		frames = aFrames;
		width = super.getWidth() / frames;
	}

	public void setActions(byte aActions) {
		actions = aActions;
		height = super.getHeight() / actions;
	}

	public void setAnSize(byte frms, byte acts) {
		frames = frms;
		actions = acts;
		setSize();

	}

	public void setAction(Actions act) {
		action = act;
	}

	// ///////////////////////////////////////

	@Override
	protected void setSize() {
		width = super.getWidth() / frames;
		height = super.getHeight() / actions;
	}

}
